package com.ly.mpayment.persist.service.debitlog;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.ly.mpayment.persist.entity.DownRechargeFlow;
import org.springframework.stereotype.Service;

import com.ly.mpayment.persist.entity.DebitLog;
import com.ly.mpayment.persist.hibernate.Page;
import com.ly.mpayment.persist.service.AbstractService;

@Service
public class DebitLogServiceImpl extends AbstractService<DebitLog, Serializable> implements IDebitLogService {

	@Override
	public boolean saveDebitLog(DebitLog log) {
		if(log != null){
			this.saveOrUpdate(log);
		}
		return false;
	}

	@Override
	public Page<DebitLog> findPageDebitLog(Map<String, Object> parameters, int start, int pageSize) {
		String hql = "select d from DebitLog d left join fetch d.downOrder where d.status = 1 #[ and d.downShopInfo.user.useruuid = :useruuid ]#  #[ and d.downOrder.dowonphonenum = :phoneNum ]#";

		if(parameters.containsKey("starttime")){
			hql += " and d.debitTime >= '"+parameters.get("starttime").toString()+" 00:00:00'";
			parameters.remove("starttime");
		}
		if(parameters.containsKey("endtime")){
			hql += " and  d.debitTime <= '"+parameters.get("endtime").toString()+" 23:59:59'";
			parameters.remove("endtime");
		}
		hql += " order by d.debitTime desc";
		return this.findPageByNamed(hql, parameters, start, pageSize);
	}

	@Override
	public List<DebitLog> findNotOrder() {
		return this.find(" select d from DebitLog d left join fetch d.downShopInfo where d.downShopInfo.downshopuuid = '00000000612e5c4e0161363c8a9c0004' and d.downOrder.downorderuuid is null");
	}

	@Override
	public long findOneDebit(String shopuuid, Date date) {
		// TODO Auto-generated method stub
		return this.findLong("select count(*) from DebitLog d where d.downShopInfo.downshopuuid = ? and d.downOrder.downorderuuid is null and d.debitTime = ?  ", shopuuid,date);
	}

	@Override
	public DebitLog findOrderLog(DownRechargeFlow flow) {
		return this.findOneObject("select d from DebitLog d where d.downOrder.downorderuuid = ?",flow.getDownorderuuid());
	}

	@Override
	public boolean updateDebitLog(DebitLog log) {
		if(log != null){
			this.updateEntity(log);
		}
		return false;
	}

}
